package com.biz.hunter.dao;

import com.biz.hunter.db.entity.Company;
import com.biz.hunter.db.entity.CompanyLinkman;
import com.biz.hunter.db.entity.CompanyLinkmanPO;
import com.biz.hunter.dbutil.BaseDAO;
import org.springframework.stereotype.Repository;

@Repository
public class CompanyLinkmanDAO {

    private static final String SQL_ADD = "insert tb_company_linkman values (null,?,?,?,?,?,?,?,?,NOW(),?,NOW())";

    public int save(Company com) {
        Long id = BaseDAO.save(SQL_ADD,com.getId(),com.getName(),com.getContact(),com.getPosition(),com.getPhone(),"","","","");
        if(id != null){
            return id.intValue();
        }
        return -1;
    }

    public CompanyLinkman getByCompanyidAndName(int id, String name) {
        String sql = "SELECT * FROM tb_company_linkman where company_id = ? and company_name =?";
        CompanyLinkmanPO po = BaseDAO.queryForObject(CompanyLinkmanPO.class, sql, id, name);
        if (po != null) {
            CompanyLinkman linkman = new CompanyLinkman();
            linkman.setId(po.getId());
            linkman.setCompanyId(po.getCompany_id());
            linkman.setCompanyName(po.getCompany_name());
            linkman.setLinkmanName(po.getLinkman_name());
            linkman.setLinkmanPosition(po.getLinkman_position());
            linkman.setLinkmanPhone(po.getLinkman_phone());
            linkman.setCreateTime(po.getCreate_time());
            linkman.setUpdateTime(po.getUpdate_time());
            return  linkman;
        }
        return null;
    }

    public int updateLinkman(Company htcompany) {
        String sql = "update tb_company_linkman set company_id = ?, company_name = ?, linkman_name = ?, linkman_position = ?, linkman_phone = ?,update_time = NOW() where company_id = ? and company_name = ?";
        return BaseDAO.execute(sql,htcompany.getId(),htcompany.getName(),htcompany.getContact(),htcompany.getPosition(),htcompany.getPhone(),htcompany.getId(),htcompany.getName());
    }
}
